package com.easy.service.impl;

import com.easy.zhu.LogMethod;
import com.easy.bean.ManagerBean;
import com.easy.mapper.ManagerBeanDao;
import com.easy.service.LoginService;
import com.easy.util.PasswordUtil;
import com.easy.util.SYS;
import com.easy.util.TokenUtil;
//import org.apache.catalina.Manager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.handler.HandlerMappingIntrospector;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    ManagerBeanDao  managerBeanDao;
    @Autowired
    private HandlerMappingIntrospector mvcHandlerMappingIntrospector;

    @Override
    @LogMethod(serviceName = "用户登录")
    public ManagerBean logininfo(String token) {
        //token中存的管理员的id
        Object id=TokenUtil.getOption(token,SYS.TOKEN_MANAGER_ID);
        if(id==null){
            throw new RuntimeException("登陆异常");
        }
        ManagerBean managerBean=managerBeanDao.one(Integer.parseInt(id.toString()));
        return managerBean;
    }

    @Override
    @LogMethod(serviceName = "用户登录")
    public String login(ManagerBean managerBean) {
        //登录逻辑
        //1通过用户民查询数据库
        ManagerBean result=managerBeanDao.getById(managerBean.getManager_id());
        if(result==null){
            throw new RuntimeException("登陆异常：");
        }
        //2验证密码
        String password=result.getLog_code();
        String encryptionPassword= result.getLog_code();
        if(!PasswordUtil.checkPassword(managerBean.getLog_code(),encryptionPassword)){
            throw new RuntimeException("登陆异常：密码错误");
        }
        //3返回结果token
        String token= TokenUtil.getToken(result.getId(),"manager");
        return token;


    }

    public static void main(String[] args) {
        String token = TokenUtil.getToken("1","admin");
        System.out.println(token);
        System.out.println(TokenUtil.verify(token));
        System.out.println(TokenUtil.getOption(token, SYS.TOKEN_MANAGER_ID));
    }
}































